﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HRMSystemShi.DAL;

namespace HRMSystemShi._2025
{
    public partial class FormOperationLog : Form
    {
        int pageNo;
        int pageSize;
        int totalLog;
        OperationLogService operationLogService;
        public FormOperationLog()
        {
            InitializeComponent();
            pageNo = 1;
            pageSize = 10;
        }

        private void FormOperationLog_Load(object sender, EventArgs e)
        {
            operationLogService = new OperationLogService();
            dataGridViewLogList.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            dataGridViewLogList.ReadOnly = true;

            comboBoxPageSize.Items.Add("10");
            comboBoxPageSize.Items.Add("20");
            comboBoxPageSize.Items.Add("30");

            totalLog = operationLogService.GetTotalCount();

            UpdateLogList();
        }

        private void linkLabelFirst_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (pageNo != 1)
            {
                pageNo = 1;
            }
            else
            {
                linkLabelFirst.Enabled = false;
            }
            UpdateLogList();
        }
        private void linkLabelLast_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (pageNo != totalLog / pageSize + 1)
            {
                pageNo = totalLog / pageSize + 1;
            }
            else
            {
                linkLabelLast.Enabled = false;
            }
            UpdateLogList();
        }

        private void linkLabelNext_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (pageNo != totalLog / pageSize + 1)
            {
                pageNo = Math.Min(pageNo + 1, totalLog / pageSize + 1);
            }
            else
            {
                linkLabelNext.Enabled = false;
            }
            UpdateLogList();
        }

        private void linkLabelPrev_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (pageNo != 1)
            {
                pageNo = Math.Max(pageNo - 1, 1);
            }
            else
            {
                linkLabelPrev.Enabled = false;
            }
            UpdateLogList();
        }
        private void comboBoxPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            pageSize = Convert.ToInt32(comboBoxPageSize.SelectedItem);
            UpdateLogList();
        }

        private void UpdateLogList()
        {
            dataGridViewLogList.DataSource = operationLogService.GetOperationLogList(pageNo, pageSize);
            labelPageNum.Text = "第" + pageNo.ToString() + "页";
            labelTotalPage.Text = "共" + (totalLog / pageSize + 1).ToString() + "页";

            if (pageNo > 1)
            {
                linkLabelFirst.Enabled = true;
                linkLabelPrev.Enabled = true;
            }
            if (pageNo < totalLog / pageSize + 1)
            {
                linkLabelLast.Enabled = true;
                linkLabelNext.Enabled = true;
            }
        }

        private void buttonJumpPageOK_Click(object sender, EventArgs e)
        {
            int pageJumpTO;
            if (int.TryParse(textBoxPageJumpTo.Text, out pageJumpTO) && pageJumpTO >= 1 && pageJumpTO <= totalLog / pageSize + 1)
            {
                pageNo = pageJumpTO;
                UpdateLogList();
            }
            else
            {
                MessageBox.Show("请输入有效页码");
            }
        }

        private void labelPageNum_Click(object sender, EventArgs e)
        {

        }
    }
}
